<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;

/**
 * 用户活动报名管理
 *
 * @icon fa fa-circle-o
 */
class Activity extends Backend
{

    /**
     * Activity模型对象
     * @var \app\common\model\user\Activity
     */
    protected $model = null;

    protected $searchFields = 'name,mobile,user.mobile';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\user\Activity;

    }

    public function index()
    {
        $activity_id = $this->request->get("activity_id", 0);
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams(null, true);
        $list = $this->model
            ->with(['user', 'activity'])
//            ->where("activity_id", $activity_id)
            ->where(function ($query) use ($activity_id) {
                if ($activity_id) {
                    $query->where("activity_id", $activity_id);
                }
            })
//            ->where($where)
            ->group('user_id,activity_id')
            ->order($sort, $order)
            ->paginate($limit);
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }
}
